<?php

namespace app\index\model;

use app\common\controller\ossStorage;
use think\Model;
use think\Request;

/*
 * insertCompany 插入公司信息
 * */
class Company extends Model
{

    /**
     * 查找公司邀请码
     * @param $data
     */
    public function getInvCode($data){
        return $this->where('invCode','=',$data)->field('company_id')->find();
    }


    /**
     *  连接user表，外键公司id，判断当前登录的用户，查询相应字段，返回数据
     * @param $phone
     * @return Company
     * @copyright hyw (接口未完善)
     */
    public function getLogoAttr($companyId)
    {
        $result = $this
            ->alias('c')
            ->where('c.company_id', '=', $companyId)
            ->join(['user' => 'u'],'u.company_id = c.company_id')
            ->where('u.position','=',2)
            ->field(['header', 'company_name'])
            ->where('c.is_delete', '=', 0)
            ->find();
        return $result;
    }

    /**
     * 连接user表，外键公司id，判断当前登录的用户，连接公司类型表，职位表，外键公司的id，查询相应的字段
     * @return Company
     * @copyright hyw
     */
    public function getInftAttr($companyId)
    {
        return $this
            ->alias('c')
            ->where('c.company_id', '=', $companyId)
            ->join(['cotype' => 'ctype'], 'ctype.company_id = c.company_id')
            ->where('ctype.is_delete', '=', 0)
            ->field(['company_name', 'type', 'company_area','com_official', 'company_introduction'])
            ->find();
    }

    /**
     * 获取公司福利，判断当前公司，查询相应字段，返回数据
     * @param $companyId
     * @return Company
     * @copyright hyw
     */
    public function getWelfAttr($companyId)
    {
        return $this
            ->where('company_id', '=', $companyId)
            ->field('company_welfare')
            ->find();
    }

    public function insertCompany($companyInf)
    {
        return $this->save($companyInf);
    }

    /**
     * 连接user表，外键公司id，判断当前登录的用户，查询对应的字段
     * @param $phone
     * @return Company[]|array|\PDOStatement|string|\think\Collection|\think\model\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @copyright hyw (接口未完善)
     */
    public function getHotCompanyAttr()
    {
        return $this
            ->alias('c')
            ->join(['cotype' => 'cot'], 'cot.company_id = c.company_id')
            ->where('cot.is_delete', '=', 0)
            ->field(['c.company_id','company_logo', 'company_name', 'company_area', 'type', 'com_official', 'range_money'])
            ->orderRand()
            ->limit(6)
            ->select();
    }

    /**
     * @return Company[]|\think\Collection
     * 返回热门公司
     * ljj
     */
    public function HotComBack()
    {
        return $this
            ->alias('com')
            ->distinct(true)
            ->join('user user','com.company_id=user.company_id')
            ->join('cotype t','com.company_id= t.company_id')
            ->where('company.is_delete','=','0')
            ->field('company.company_id,company_logo,type,company_name,range_money')
            ->orderRand()
            ->limit(8)
            ->select();
    }


    /**
     * @param $user
     * @return Company
     * 是否注册公司
     */
    public function isRegisterCompany($user)
    {
        return $this
            ->alias('c')
            ->join('user u','u.company_id=c.company_id')
            ->where(['u.user_id' => $user,'u.is_delete'=>0])
            ->find();
    }

    public function getCompanyAddressAttr($companyId)
    {
        return $this->where('company_id','=',$companyId)->field('company_area')->find();
    }

    /**
     * @param $searchValue
     * @param $page
     * @param $searchType
     * @param $urban
     * @param $town
     * @param $province
     * @return Company[]|\think\Collection
     * 查看搜索值
     */
    public function checkSearch($searchValue,$page=1,$searchType='',$urban='',$town='',$province='')
    {
        return $this->alias('com')
            ->join('cotype c','c.company_id=com.company_id')
            ->join('user u','u.company_id=com.company_id')
            ->where('company_name','like','%'.$searchValue.'%')
            ->where('company_area','like','%'.$urban.'%')
            ->where('company_area','like','%'.$town.'%')
            ->where('company_area','like','%'.$province.'%')
            ->where('c.type','like','%'.$searchType.'%')
            ->where('u.position','=','2')
            ->where(['com.is_delete'=>0])
            ->field('com.*,c.*,u.username,u.header')
            ->select();
    }

    /**
     * @param $searchValue
     * @param $page
     * @param $searchType
     * @param $urban
     * @param $town
     * @param $province
     * @return Company|\think\Paginator
     * 搜索公司
     */
    public function getSearchCompanyAttr($searchValue,$page=1,$searchType='',$urban='',$town='',$province='')
    {
        return $this->alias('com')
            ->join('cotype c','c.company_id=com.company_id')
            ->join('user u','u.company_id=com.company_id')
            ->where('company_name','like','%'.$searchValue.'%')
            ->where('company_area','like','%'.$urban.'%')
            ->where('company_area','like','%'.$town.'%')
            ->where('company_area','like','%'.$province.'%')
            ->where('c.type','like','%'.$searchType.'%')
            ->where('u.position','=','2')
            ->where(['com.is_delete'=>0])
            ->field('com.*,c.*,u.username,u.header')
            ->paginate(15,false,['page'=>$page]);
    }

    /**
     * @param $page
     * @param $searchType
     * @param $urban
     * @param $town
     * @param $province
     * @return Company|\think\Paginator
     * 返回公司所有值
     */
    public function getAllCompany($page=1,$searchType='',$urban='',$town='',$province='')
    {
        return $this->alias('com')
            ->join('cotype c','c.company_id=com.company_id')
            ->join('user u','u.company_id=com.company_id')
            ->where('company_area','like','%'.$urban.'%')
            ->where('company_area','like','%'.$town.'%')
            ->where('company_area','like','%'.$province.'%')
            ->where('c.type','like','%'.$searchType.'%')
            ->where('u.position','=','2')
            ->where(['com.is_delete'=>0])
            ->field('com.*,c.*,u.username,u.header')
            ->paginate(15,false,['page'=>$page]);
    }

    /**
     * @param $companyId
     * @return Company[]|\think\Collection
     * 返回公事人事
     * ljj
     */
    public function getUserPosition($companyId)
    {
        return $this->alias('com')
                    ->join('user u','u.company_id=com.company_id')
                    ->join('certificate c','c.user_id=u.user_id')
                    ->where('u.position','=','1')
                    ->field('c.mechanism,u.header,u.username,com.company_id')
                    ->limit('3')
                    ->select();
    }

    public function getCompany($company_id)
    {
        return $this->alias('com')
                    ->where('company_id','=',$company_id)
                    ->find();
    }
}